package com.ruoyi.exam.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.exam.domain.vo.ExamPaperVo;
import com.ruoyi.system.domain.TTextContent;
import com.ruoyi.system.mapper.TTextContentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.exam.mapper.TExamPaperMapper;
import com.ruoyi.exam.domain.TExamPaper;
import com.ruoyi.exam.service.ITExamPaperService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测评试卷Service业务层处理
 *
 * @author Bandit
 * @date 2022-11-01
 */
@Service
public class TExamPaperServiceImpl implements ITExamPaperService
{
    @Autowired
    private TExamPaperMapper tExamPaperMapper;

    @Autowired
    private TTextContentMapper tTextContentMapper;

    /**
     * 查询测评试卷
     *
     * @param id 测评试卷主键
     * @return 测评试卷
     */
    @Override
    public TExamPaper selectTExamPaperById(Long id)
    {
        return tExamPaperMapper.selectTExamPaperById(id);
    }

    @Override
    public ExamPaperVo selectExamPaperVoById(Long id) {
        ExamPaperVo examPaperVo = tExamPaperMapper.selectExamPaperVoById(id);
        examPaperVo.setQuestionArrays(JSONArray.parseArray(examPaperVo.getContent()).toList(Long.class));
        return examPaperVo;
    }

    /**
     * 查询测评试卷列表
     *
     * @param tExamPaper 测评试卷
     * @return 测评试卷
     */
    @Override
    public List<TExamPaper> selectTExamPaperList(TExamPaper tExamPaper)
    {
        return tExamPaperMapper.selectTExamPaperList(tExamPaper);
    }

    @Override
    public List<ExamPaperVo> selectExamPaperVoList(ExamPaperVo examPaperVo) {
        List<ExamPaperVo> list = tExamPaperMapper.selectExamPaperVoList(examPaperVo);
        return list.stream().peek(examPaper -> {
           examPaper.setQuestionArrays(JSONArray.parseArray(examPaper.getContent()).toList(Long.class));
        }).collect(Collectors.toList());
    }

    /**
     * 新增测评试卷
     *
     * @param tExamPaper 测评试卷
     * @return 结果
     */
    @Override
    public int insertTExamPaper(TExamPaper tExamPaper)
    {
        tExamPaper.setCreateTime(DateUtils.getNowDate());
        return tExamPaperMapper.insertTExamPaper(tExamPaper);
    }

    @Override
    @Transactional
    public int insertExamPaperVo(ExamPaperVo examPaperVo) {
        TExamPaper tExamPaper = new TExamPaper();
        BeanUtils.copyProperties(examPaperVo, tExamPaper);
        List<Long> questionArrays = examPaperVo.getQuestionArrays();
        // 构造内容
        TTextContent tTextContent = new TTextContent();
        tTextContent.setContent(JSONArray.toJSONString(questionArrays));
        tTextContent.setCreateTime(new Date());
        tTextContentMapper.insertTTextContent(tTextContent);
        // 插入测评试卷
        tExamPaper.setContentId(tTextContent.getId());
        tExamPaper.setCreatedBy(SecurityUtils.getUserId());
        tExamPaper.setCreateTime(new Date());
        return tExamPaperMapper.insertTExamPaper(tExamPaper);
    }

    /**
     * 修改测评试卷
     *
     * @param tExamPaper 测评试卷
     * @return 结果
     */
    @Override
    public int updateTExamPaper(TExamPaper tExamPaper)
    {
        return tExamPaperMapper.updateTExamPaper(tExamPaper);
    }

    @Override
    @Transactional
    public int updateExamPaperVo(ExamPaperVo examPaperVo) {
        TExamPaper tExamPaper = new TExamPaper();
        BeanUtils.copyProperties(examPaperVo, tExamPaper);
        // 更新内容如果不为空
        List<Long> questionArrays = examPaperVo.getQuestionArrays();
        if (questionArrays != null && questionArrays.size() != 0) {
            TTextContent textContent = new TTextContent();
            textContent.setId(examPaperVo.getContentId());
            textContent.setContent(JSONArray.toJSONString(examPaperVo.getQuestionArrays()));
            tTextContentMapper.updateTTextContent(textContent);
        }
        // 更新ExamPaper
        return tExamPaperMapper.updateTExamPaper(tExamPaper);
    }

    /**
     * 批量删除测评试卷
     *
     * @param ids 需要删除的测评试卷主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExamPaperByIds(Long[] ids)
    {
        // 删除试卷级联删除内容
        Long[] contentIds = tExamPaperMapper.selectContentIdsByIds(ids);
        tTextContentMapper.deleteTTextContentByIds(contentIds);
        return tExamPaperMapper.deleteTExamPaperByIds(ids);
    }

    /**
     * 删除测评试卷信息
     *
     * @param id 测评试卷主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTExamPaperById(Long id)
    {
        // 级联删除内容
        TExamPaper tExamPaper = tExamPaperMapper.selectTExamPaperById(id);
        tTextContentMapper.deleteTTextContentById(tExamPaper.getContentId());
        return tExamPaperMapper.deleteTExamPaperById(id);
    }
}
